/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "PlayerSession.h"
#include "PQException.h"

namespace PQServiceLogin
{
	int PlayerSession::totalCount=0;
	PlayerSession::PlayerSession(SOCKET socket) : _socket(0),_isConnected(false)
	{
		_sessionId=totalCount;
		totalCount++;

		_socket=new PQNet::TCPSocket(socket);
		_socket->setNonBlocking();
		_socket->setLinger(0);

		_inputStream=new PQNet::NetInputStream(_socket);
		_outputStream=new PQNet::NetOutputStream(_socket);

		//init
		_isConnected=false;
		_playerState=Player_State_LoginConnect;
		//reset kick time

		//set connect time
		//_connectTime=getCurrentTime();

	}

	PlayerSession::~PlayerSession()
	{
	}

	void PlayerSession::update(float deltaT)
	{
	}

	SOCKET PlayerSession::getSocketHandle()
	{
		if(_socket){
			return _socket->getSocketHandle();
		}else{
		return INVALID_SOCKET;
		}
	}

	PQNet::TCPSocket* PlayerSession::getSocket()
	{
		return _socket;
	}


	bool PlayerSession::processInput()
	{
		if(!_isConnected){
			return true;//not connected don't process input
		}

		//start work from here
		if(!_inputStream->fill()){
			return false;
		}
		return true;
	}

	bool PlayerSession::processOutput()
	{
		PQThrowNotImpl();
		return false;
	}


	bool PlayerSession::processCommand()
	{
		PQThrowNotImpl();
		return false;
	}


	void PlayerSession::cleanUp()
	{
		PQThrowNotImpl();
	}


	void PlayerSession::setIsConnected(bool connected)
	{
		_isConnected=connected;
	}


	void PlayerSession::setPlayerState(PlayerState state)
	{
		_playerState=state;
	}

	PlayerState PlayerSession::getPlayerState()
	{
		return _playerState;
	}


}

